package com.primary.utils;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.Date;

/**
 * @author シン(Shen)
 * @time 2023/06/24 20:38 Saturday
 * @description TODO
 * @return
 */
public class JwtUtils {
    /* After 7 days it will be useless */
    private static final long expire=604800;
    /* personal secret 32 bits */
    private static final String secret="kyouhadoyoubiihgrksicuxanfysjf";
    public static String generateToken(String username){
        Date now=new Date();
        Date expiration=new Date(now.getTime()+1000*expire);
        return Jwts.builder()
                .setHeaderParam("type","JWT")
                .setSubject(username)
                .setIssuedAt(now)
                .setExpiration(expiration)
                .signWith(SignatureAlgorithm.HS512,secret)
                .compact();
    }

    /* check token */
    public static Claims getClaimsByToken(String token){
        return Jwts.parser()
                .setSigningKey(secret)
                .parseClaimsJws(token)
                .getBody();
    }
}
